#!/bin/sh
#抽取各个维度表数据到dim表中
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE-$0}")"; pwd)
UP_DIR=$(dirname $(dirname $(dirname "$PWD")))
echo ${UP_DIR}
source ${UP_DIR}/system_env.sh


START_TIME=$(date +%s)
hive_db=${hive_db}

if [ -z "${hive_db}" ];then
  hive_db=dataplatform_trip
else
  hive_db=${hive_db}
fi


if [ -z "$1" ];then
  part_time=$(date -d "1 days ago" +%Y%m%d)
else
  part_time=$1
fi

sql_suffix=$(cat <<EOF
        insert overwrite table ${hive_db}.dim_strains
         select row_number() over(partition by 1),
          strains
           from ${hive_db}.ods_business_car_type
           where strains is not null
           group by strains;
EOF
)
sql_suffix1=$(cat <<EOF
        insert overwrite table ${hive_db}.dim_driver_type
         select row_number() over(partition by 1),
          driver_type
           from ${hive_db}.ods_business_car_type
           where driver_type is not null
           group by driver_type;
EOF
)
sql_suffix2=$(cat <<EOF
        insert overwrite table ${hive_db}.dim_horse_power
         select row_number() over(partition by 1),
           horse_power
           from ${hive_db}.ods_business_car_type
           where horse_power is not null
           group by horse_power;
EOF
)
sql_suffix3=$(cat <<EOF
        insert overwrite table ${hive_db}.dim_bridge_speed_ratio
         select row_number() over(partition by 1),
          bridge_speed_ratio
           from ${hive_db}.ods_business_car_type
           where bridge_speed_ratio is not null
           group by bridge_speed_ratio;
EOF
)
sql_suffix4=$(cat <<EOF
        insert overwrite table ${hive_db}.dim_emission_standards
         select row_number() over(partition by 1),
          emission_standards
           from ${hive_db}.ods_business_car_type
            where emission_standards is not null
           group by emission_standards;
EOF
)

CUR_DIR=$(cd "$(dirname "$0")"; pwd)
hive_config=$(cat  "${CUR_DIR}"/hive.config)
sql="${hive_config}${sql_suffix}"
sql1="${hive_config}${sql_suffix1}"
sql2="${hive_config}${sql_suffix2}"
sql3="${hive_config}${sql_suffix3}"
sql4="${hive_config}${sql_suffix4}"
echo "导入dws_urea_so_sar_pdi的SQL为：${sql}"

#执行语句·
hive -e "${sql}"
if [ $? -ne 0 ];then
  echo "导入dim_strains失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入dim_strains成功，耗时$((`date +%s`-START_TIME))秒..."
fi

hive -e "${sql1}"
if [ $? -ne 0 ];then
  echo "导入dim_driver_type失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入dim_driver_type成功，耗时$((`date +%s`-START_TIME))秒..."
fi

hive -e "${sql2}"
if [ $? -ne 0 ];then
  echo "导入dim_horse_power失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入dim_horse_power成功，耗时$((`date +%s`-START_TIME))秒..."
fi

hive -e "${sql3}"
if [ $? -ne 0 ];then
  echo "导入dim_bridge_speed_ratio失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入dim_bridge_speed_ratio成功，耗时$((`date +%s`-START_TIME))秒..."
fi

hive -e "${sql4}"
if [ $? -ne 0 ];then
  echo "导入dim_strains失败，耗时$((`date +%s`-START_TIME))秒..."
else
  echo "导入dim_strains成功，耗时$((`date +%s`-START_TIME))秒..."
fi